package com.crm.miaohe.controller.customer;

import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.authentication.response.user.AuthUserDetail;
import com.crm.miaohe.controller.customer.request.CustomerBatchUpdateParams;
import com.crm.miaohe.controller.customer.request.CustomerParams;
import com.crm.miaohe.controller.customer.request.CustomerQueryParams;
import com.crm.miaohe.controller.customer.response.CustomerOverview;
import com.crm.miaohe.model.TblCustomer;
import com.crm.miaohe.service.CustomerService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/erp/customer")
public class CustomerController {

    private final CustomerService customerService;

    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @PostMapping("/apiRegister")
    public ResponseEntity<Void> apiRegisterCustomer(@RequestBody @Valid CustomerParams params) {
        customerService.addCustomer(params);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/register")
    public ResponseEntity<Void> registerCustomer(@RequestBody @Valid CustomerParams params) {
        customerService.addCustomer(params);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/update")
    public ResponseEntity<Void> updateCustomer(@RequestBody @Valid CustomerParams params) {
        customerService.updateCustomer(params);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/batchUpdate")
    public ResponseEntity<Object> batchUpdateCustomer(@RequestBody @Valid CustomerBatchUpdateParams params) {
        return ResponseEntity.ok(customerService.batchUpdateCustomer(params));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deleteCustomer(@PathVariable Long id) {
        customerService.deleteCustomer(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/lock/{id}")
    public ResponseEntity<Void> lockCustomer(@PathVariable Long id) {
        customerService.lockCustomer(id);
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/deposit/{id}/{amount}")
    public ResponseEntity<Void> updateDeposit(@PathVariable Long id, @PathVariable Double amount) {
        customerService.updateDeposit(id, amount);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/unlock/{id}")
    public ResponseEntity<Void> unlockCustomer(@PathVariable Long id) {
        customerService.unlockCustomer(id);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/apiApproved")
    public ResponseEntity<Void> apiApprovedCustomer(@RequestBody @Valid CustomerParams params) {
        customerService.updateCustomer(params);
        customerService.approvedCustomer(params.getId());
        return ResponseEntity.ok().build();
    }

    @PutMapping("/approved/{id}")
    public ResponseEntity<Void> approvedCustomer(@PathVariable Long id) {
        customerService.approvedCustomer(id);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/resetPwd")
    public ResponseEntity<Void> resetCustomerPwd(@RequestBody CustomerParams params) {
        customerService.updatePassword(params);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/checkPhoneNumber")
    public ResponseEntity<Boolean> checkPhoneNumber(CustomerParams params) {
        return ResponseEntity.ok(customerService.checkPhoneNumber(params));
    }

    @GetMapping("/checkUserName")
    public ResponseEntity<Boolean> checkUserName(CustomerParams params) {
        return ResponseEntity.ok(customerService.checkUserName(params));
    }

    @GetMapping("/list")
    public ResponseEntity<PageResponse<TblCustomer>> overview(@Valid CustomerQueryParams params) {
        return ResponseEntity.ok(customerService.getCustomers(params));
    }

    @GetMapping("/details")
    public ResponseEntity<TblCustomer> currentAuthUser() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return ResponseEntity.ok(customerService.currentUser(username));
    }

    @GetMapping("/customer/{username}")
    public ResponseEntity<TblCustomer> getCustomerInfo(@PathVariable String username) {
        return ResponseEntity.ok(customerService.currentUser(username));
    }

    @GetMapping("/approved")
    public ResponseEntity<List<TblCustomer>> getApprovedCustomers() {
        return ResponseEntity.ok(customerService.getApprovedCustomers());
    }
}
